// 润乾报表公共组件
import React from 'react';
import { $http } from 'containers/config/https';
import { httpConfig } from 'containers/config/httpConfig';
class reports extends React.Component {
    constructor(props) {
        super(props);
        let session = window.sessionStorage.userData ? JSON.parse(window.sessionStorage.userData) : {};
        let sessionUserID = session.userID;
        let sessionUserName = session.userName;
        let sessionLocID = session.locID;
        let sessionLocDesc = session.locDesc;
        let sessionGroupID = session.groupID;
        let sessionGroupDesc = session.groupDesc;
        let sessionHospID = session.hospDesc;
        let sessionHospDesc = session.hospDesc;
        let sessionNowHospID = session?.hospID || '';
        let sessionStr = "&sessionUserID=" + sessionUserID + "&sessionUserName=" + sessionUserName + "&sessionLocID=" + sessionLocID + "&sessionLocDesc=" + sessionLocDesc + "&sessionGroupID=" + sessionGroupID + "&sessionGroupDesc=" + sessionGroupDesc + "&sessionHospID=" + sessionHospID + "&sessionHospDesc=" + sessionHospDesc + "&sessionNowHospID=" + sessionNowHospID;
        this.sessionStr = sessionStr;
        this.state = {
            path: '',
            //debugPath:"http://localhost:6868/demo/reportJsp/preview.jsp?rpxHome=C%3A%2FUsers%2Fxys%2FDesktop%2F报表&dfxHome="   //本地预览路径
            debugPath: "http://localhost:6868/demo/reportJsp/preview.jsp?rpxHome=C:/Users/xys/Desktop/润乾安装文件&dfxHome="    //^头要转成encode格式
        }
    }
    componentDidMount() {
        if (!this.props.debug) {
            this.getRaqSoftPath();
        }
    }
    //获取润乾服务器路径
    getRaqSoftPath = () => {
        $http.post('urlS', this, {
            code: "01040114",
            data: {
                params: [{ appCode: '' }]
            },
            success: function (res) {
                if (res.errorCode == 0) {
                    this.setState({ path: res.result.path });
                } else {
                    message.error("没数据");
                }
            }
        });
    }
    getUrl = () => {
        let url = "";
        let splitUrl = "";
        let raqName = this.props.raqName ? this.props.raqName : "";
        const params = this.props.params ? this.props.params : {};
        let paramStr = "";
        for (let key in params) {
            paramStr = paramStr + "&" + key + "=" + params[key];
        }
        if (raqName.indexOf(".rpx") === -1) {
            raqName = raqName + ".rpx";
        }
        if (this.state.path) {
            if (this.state.path.indexOf("//") > -1) {
                let newUrl = this.state.path.split("//")[1];
                if (newUrl.indexOf("/") > -1) {
                    for (var i = 1; i < newUrl.split("/").length; i++) {
                        splitUrl = splitUrl + "/" + newUrl.split("/")[i];
                        // if(splitUrl===''){
                        //     splitUrl=newUrl.split("/")[i];
                        // }else{
                        //     splitUrl=splitUrl+"/"+newUrl.split("/")[i];
                        // }
                    }
                } else {
                    splitUrl = "/" + newUrl;
                }
            } else {
                splitUrl = this.state.path;
            }
            // url=window.location.protocol+"//"+window.location.host+splitUrl+'?rpx='+raqName+paramStr+this.sessionStr;
            let windowHost = window.location && window.location.protocol && window.location.host ? (window.location.protocol + '//' + window.location.host) : (window.location && window.location.origin ? window.location.origin : '');
            // 判断是否是本地，如果是本地的话取https配置地址，线上则取地址栏地址 （内外网）
            let httpUrl = windowHost && (windowHost.indexOf('localhost') !== -1 || windowHost.indexOf('127.0.0.1') !== -1) ? (httpConfig.ipDeault + '/demo/reportJsp/showReport.jsp') : (window.location.protocol + '//' + window.location.host + splitUrl);
            url = httpUrl + '?rpx=' + raqName + paramStr + this.sessionStr;
        } else if (this.state.debugPath) {
            url = this.state.debugPath + '&rpx=' + raqName + paramStr + this.sessionStr;
        }
        return encodeURI(url);
    }
    render() {
        return (
            <div style={{ height: '99%' }}>
                <iframe
                    id={this.props.id || ''}
                    style={{ width: '100%', border: '0px', height: '100%' }}
                    // sandbox="allow-scripts allow-forms allow-same-origin"
                    scrolling="auto"
                    src={this.getUrl()}
                />
            </div>
        );
    }
};
export default reports;
